/*
 * Copyright 2011 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.pc.ui.client;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.Map;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JToolBar;
import org.opu.pc.ui.client.actions.AbstractAction;
import org.opu.pc.ui.util.JProperties;
import org.opu.pc.ui.util.Logger;
import org.opu.pc.ui.util.ResourceHandle;

/**
 *
 * @author yura
 */
public class MainToolBar extends JToolBar {

	private static final long serialVersionUID = -6426450607737012646L;
	
	public static final String MAIN_PROP = "ToolBar";
    public static final String TOOLBAR_PATH = "/org/opu/pc/ui/resource/ToolBar.properties";

    public static final String BORDER_PAINTED_PROP = "BorderPainted";
    public static final String CONTENT_AREA_FILLED_PROP = "ContentAreaFilled";
    public static final String HIDE_ACTION_TEXT_PROP = "HideActionText";
    public static final String HORIZONTAL_TEXT_POSITION_PROP = "HorizontalTextPosition";
    public static final String VERTICAL_TEXT_POSITION_PROP = "VerticalTextPosition";
    public static final String ROLLOVER_MOUSE_LISTENER_PROP = "RolloverMouseListener";
    public static final String FOCUS_PAINTED_PROP = "FocusPainted";

    public static final MouseAdapter MOUSE_LISTENER = new RolloverMouseListener();
    public static final JProperties prop = new JProperties();

    static {
        ResourceHandle.loadProperties(prop, TOOLBAR_PATH);
    }

    Logger logger = Logger.create(this.getClass());

    public MainToolBar() {
        super(ResourceHandle.getApplicationName() + " toolbar", HORIZONTAL);
    }
    
    public void initComponent(Map<String, AbstractAction> actions) {
        initComponent(actions, prop);
    }

    public void initComponent(Map<String, AbstractAction> actions, JProperties localProp) {
        logger.debug("Load properties: " + localProp);

        String data = localProp.getProperty(MAIN_PROP);
        logger.debug("Property: " + data);

        String[] split = data.split(";");
        for (String str : split) {
            AbstractAction get = actions.get(str != null ? str.trim() : "");
            if (get != null) {
                add(get);
            }
        }

        logger.debug("Complete " + Arrays.toString(split));
    }

    @Override
    protected JButton createActionComponent(Action a) {
        JButton button = super.createActionComponent(a);
        initBtn(button);
        return button;
    }

    private void initBtn(JButton button) {
        button.setHideActionText(prop.getBoolean(HIDE_ACTION_TEXT_PROP, false));

        button.setBorderPainted(prop.getBoolean(BORDER_PAINTED_PROP, false));
        button.setContentAreaFilled(prop.getBoolean(CONTENT_AREA_FILLED_PROP, true));
        if (prop.getBoolean(ROLLOVER_MOUSE_LISTENER_PROP, false)){
            button.addMouseListener(MOUSE_LISTENER);
        }

        button.setHorizontalTextPosition(prop.getInt(HORIZONTAL_TEXT_POSITION_PROP, RIGHT));
        button.setVerticalTextPosition(prop.getInt(VERTICAL_TEXT_POSITION_PROP, CENTER));
        button.setFocusPainted(prop.getBoolean(FOCUS_PAINTED_PROP, true));
    }

    static class RolloverMouseListener extends MouseAdapter {

        @Override
        public void mouseEntered(MouseEvent e) {
            Object source = e.getSource();
            if (source instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) source;
                if (button.isEnabled()) {
                    button.setContentAreaFilled(true);
                    button.setBorderPainted(true);
                }
            }
        }

        @Override
        public void mouseExited(MouseEvent e) {
            Object source = e.getSource();
            if (source instanceof AbstractButton) {
                AbstractButton button = (AbstractButton) source;
                if (button.isEnabled()) {
                    button.setContentAreaFilled(false);
                    button.setBorderPainted(false);
                }
            }
        }
    }
}
